<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/10
 * Time: 15:29
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\lib\ArrClass;
use app\common\lib\Show;
use app\common\lib\Status;
use think\Exception;
use think\facade\Filesystem;

class Upload extends Base
{
    public function file()
    {
        if (!$this->request->isPost()) {
            throw new Exception("请求类型错误", 400);
        }
        $file = $this->request->file();
        if (empty($file)) {
            return Show::json_show(Status::status_error, "请上传图片");
        }
        $key = array_keys($file);
        //$res = Filesystem::disk('public')->putFile("upload/".$key[0], $this->request->file('image'));
        $arrType = ArrClass::uploadAttr();
        try {
            $objClass = ArrClass::initClass($key[0], $arrType, [], true);
            $res = $objClass->upload($this->request);
        }catch (\Exception $e){
            return Show::json_show(Status::status_error,$e->getMessage());
        }
        if($res){
            return Show::json_show(Status::status_normal,"上传成功",[
                "url"=>$res
            ]);
        }else{
            return Show::json_show(Status::status_error,'上传失败！请重试');
        }
    }
}